﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DFCLib;

namespace CW_Move_Format_Guidelines
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {

        private IDfClientX m_DfClientX = null;
        private schemaFormatGuidelines.FormatGuidelinesDataTable m_FormatGuidelinesTable = new schemaFormatGuidelines.FormatGuidelinesDataTable();


        public Window1()
        {
            InitializeComponent();
        }

        private void m_doButton_Click(object sender, RoutedEventArgs e)
        {
            String dqlStr = "SELECT object_name, r_object_id, i_folder_id ";
            dqlStr += "FROM dm_document ";
            dqlStr += "WHERE i_cabinet_id = '0c01b1df8000250d' ";
            dqlStr += "and a_content_type like '%excel%' and i_folder_id IS NOT NULL ";
            dqlStr += "ENABLE(ROW_BASED)";


            m_DfClientX = new DfClientX();

            IDfSessionManager smgr = null;
            IDfClient client = null;
            IDfLoginInfo loginInfo = null;
            IDfQuery query = null;


            try
            {
                client = m_DfClientX.getLocalClient();

                smgr = client.newSessionManager();
                loginInfo = m_DfClientX.getLoginInfo();
                loginInfo.setUser("your user");
                loginInfo.setPassword("your password");
                smgr.setIdentity("DMS", loginInfo);

                IDfSession session = smgr.getSession("DMS");

                query = m_DfClientX.getQuery();
                query.setDQL(dqlStr);

                IDfCollection rs = query.execute(session, (int)tagDfQueryTypes.DF_READ_QUERY);

                int iColCount = rs.getAttrCount();

                while (rs.next())
                {
                    String objName = rs.getString("object_name");
                    IDfId objId = rs.getId("r_object_id");
                    IDfId parentFolderId = rs.getId("i_folder_id");

                    dqlStr = "SELECT r_folder_path FROM dm_folder WHERE r_object_id = '" + parentFolderId.toString() + "' ENABLE(ROW_BASED)";

                    query.setDQL(dqlStr);

                    IDfCollection folderPaths = query.execute(session, (int)tagDfQueryTypes.DF_READ_QUERY);
                    String orgFolderPath = null;

                    while (folderPaths.next())
                    {
                        String currFolderPath = folderPaths.getString("r_folder_path");
                        if (!String.IsNullOrEmpty(currFolderPath))
                        {
                            orgFolderPath = currFolderPath;
                        }
                                                
                    }

                    String newFolderPath = orgFolderPath.Replace("Longform Video Content", "Long Form Video Content");

                    IDfDocument currFormatGuideline = (IDfDocument) session.getObject(objId);
                    String errors = null;

                    try
                    {
                        currFormatGuideline.link(newFolderPath);
                        currFormatGuideline.unlink(orgFolderPath);
                    }
                    catch (Exception ex)
                    {
                        //MessageBox.Show(ex.ToString());
                        errors = ex.ToString();
                    }

                    schemaFormatGuidelines.FormatGuidelinesRow newRow = m_FormatGuidelinesTable.NewFormatGuidelinesRow();
                    newRow.Name = objName;
                    newRow.ID = objId.toString();
                    newRow.OrgFolderPath = orgFolderPath;
                    newRow.NewFolderPath = newFolderPath;
                    newRow.Errors = errors;

                    m_FormatGuidelinesTable.AddFormatGuidelinesRow(newRow);

                }

            
            }
            catch (Exception er)
            {
                MessageBox.Show(er.ToString());
            }


        }
    }
}
